package com.subject.shuati.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.subject.shuati.application.covert.SubjectCategoryDtoConverter;
import com.subject.shuati.application.covert.SubjectLabelDtoConverter;
import com.subject.shuati.application.dto.SubjectCategoryDto;
import com.subject.shuati.application.dto.SubjectLabelDto;
import com.subject.shuati.common.entity.Result;
import com.subject.shuati.domain.entity.SubjectCategoryBo;
import com.subject.shuati.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 分类控制层
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 新增分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryName(),"分类名不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(),"分类父级id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.Instance.covertBoCategory(subjectCategoryDto);
            subjectCategoryDomainService.addSubject(subjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}"+e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 更新分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getId(),"分类ID不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.Instance.covertBoCategory(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error:{}"+e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 分类逻辑删除
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getId(),"分类ID不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.Instance.covertBoCategory(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}"+e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询岗位大类
     */
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        try {
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.Instance.
                    covertBoCategory(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDto> subjectCategoryDTOList = SubjectCategoryDtoConverter.Instance.
                    convertBoToCategoryDTOList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 根据分类id查二级分类
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}"
                        , JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "分类id不能为空");
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.Instance.
                    covertBoCategory(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBOList =
                    subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDto> subjectCategoryDTOList = SubjectCategoryDtoConverter.Instance.
                    convertBoToCategoryDTOList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 查询分类及标签一次性
     */
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDto>> queryCategoryAndLabel(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryCategoryAndLabel.dto:{}"
                        , JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "分类id不能为空");
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.Instance.
                    covertBoCategory(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBOList = subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBO);
            List<SubjectCategoryDto> dtoList = new LinkedList<>();
            subjectCategoryBOList.forEach(bo -> {
                SubjectCategoryDto dto = SubjectCategoryDtoConverter.Instance.covertBoToDTO(bo);
                List<SubjectLabelDto> labelDTOList = SubjectLabelDtoConverter.INSTANCE.converterToLabelDtoList(bo.getLabelBOList());
                dto.setLabelDtoList(labelDTOList);
                dtoList.add(dto);
            });
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }
}
